package com.tbrain.secretFriend.util;
import java.sql.*;
import java.util.List;
import com.tbrain.secretFriend.model.User;
import com.tbrain.secretFriend.service.UserService;

public class ConnectionHelper {

	static{
		try {
//			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
//            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection(){
		Connection conn=null;
		try {
//			conn = DriverManager.getConnection("jdbc:odbc:secretFriend");
//            conn = DriverManager.getConnection("jdbc:derby://localhost:1527/SF", "secretFriend", "sf");
//			conn = DriverManager.getConnection("jdbc:mysql://63.247.81.211:3306/tobalyah11017com1052_tbrain?user=ctobal&password=Tobal000");
            //conn = DriverManager.getConnection("jdbc:mysql://tbrain.com:3306/tobalyah11017com1052_tbrain","ctobal", "Tobal000");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/ctobal_secretfriend","ctobal_secret", "Tobal0000");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void main(String[] args){
		try{
//			Class.forName("com.mysql.jdbc.Driver").newInstance();
//			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			System.out.println("Obteniendo Conexion...");
//			Connection conn = DriverManager.getConnection("jdbc:mysql://tbrain.com/comtbrai_secretfriend","comtbrai_admin","tobal00");
//			Connection conn = DriverManager.getConnection("jdbc:mysql://www.tbrain.com/comtbrai_secretfriend?user=comtbrai_admin&password=tobal00");
//			Connection conn = DriverManager.getConnection("jdbc:odbc:secretFriend");
			Connection conn = ConnectionHelper.getConnection();
			System.out.println("Conexion Obtenida...");
			if(conn!=null){
				if(!conn.isClosed()){
					System.out.println("Conexion Exitosa");
					UserService userService = new UserService(conn);
					userService.calculateAllSecretFriend();
					List users = userService.selectUsers();
					System.out.println("******************************************");
					for(int i=0;i<users.size();i++){
						User u = (User)users.get(i);
						User secret = userService.selectSecretFriend(u);

						System.out.println(u+"\t--->"+secret);
					}
				}else{
					System.out.println("Conexion Fallida");
				}
			}else{
				System.out.println("No regreso conexion");
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

